/*
 * Copyrignt (c) qkier.com. All Rights Reserved.
 * Author: maozhihua@qkier.com
 * Created Time: 2017-07-24 15:56:33
 * Last Modified: 2017-07-24 16:02:10
 * File Name: userinfo.go
 * Description:
 */

package controllers

import (
	"passport/avatar"
	"passport/errorcode"
	"passport/user"
	"github.com/garyburd/redigo/redis"
	log "github.com/cihub/seelog"
	"passport/account"
)

type UserInfoController struct {
	URLSignController
}

/*
	用户信息查询,后台使用,不印证登录
*/
func (p *UserInfoController) QueryUserInfo() {
	defer p.Output("QueryUserProfile")

	userid := p.GetString("uid", "")
	ui, uiErr := user.GetUserService().GetAllUserInfo(userid)
	if uiErr != nil {
		log.Errorf("query user profile failed for uid: %s, msg: %s", userid, uiErr.Error())
		p.Resp["code"] = errorcode.EC_GL_INTERNAL_ERROR
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INTERNAL_ERROR)
		return
	}

	if ui == nil {
		log.Errorf("query user profile get empty for uid: %s", userid)
		p.Resp["code"] = errorcode.EC_GL_INTERNAL_ERROR
		p.Resp["msg"] = "该用户不存在"
		return
	}
	p.Resp["code"] = errorcode.EC_GL_SUCCESS
	p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
	p.Resp["mobile"] = ui.Mobile
	p.Resp["nick_name"] = ui.NickName
	p.Resp["gender"] = ui.Gender
	if len(ui.Avatar) == 32 { //自有的
		p.Resp["avatar"], _ = avatar.GetAvatarURL(ui.Avatar)
	} else { //第三方地址
		p.Resp["avatar"] = ui.Avatar
	}
	p.Resp["slogan"] = ui.Slogan
	p.Resp["region_code"] = ui.RegionCode
	p.Resp["region_addr"] = ui.RegionAddr
	p.Resp["birthday"] = ui.Birthday
	p.Resp["invite_code"] = ui.InviteCode
	p.Resp["invite_by"] = ui.InviteBy
	p.Resp["vuid"] = ui.Vuid
	p.Resp["reg_time"] = ui.InsertTime
	p.Resp["reg_plat"] = ui.RegPlat
}

func (p *UserInfoController) QueryUserThirdInfo()  {
	defer p.Output("QueryUserThirdInfo")

	userid := p.GetString("uid", "")
	userBinded, userBindedErr := account.GetAccountBindService(). GetBindedAccountUserid(userid)
	if userBindedErr != nil {
		log.Warnf("GetBindedAccountForUserid error: %s, userid: %s", userBindedErr.Error(), userid)
		p.Resp["code"] = errorcode.EC_GL_INTERNAL_ERROR
		p.Resp["msg"] = "系统内部错误"
		return
	}
	if userBinded == nil {
		log.Errorf("query user profile get empty for uid: %s", userid)
		p.Resp["code"] = errorcode.EC_GL_INTERNAL_ERROR
		p.Resp["msg"] = "该用户不存在"
		return
	}
	log.Infof("userBinded:%s",userBinded)
	var userinfo user.ThirdAccountUserInfo
	//userinfo:=&user.ThirdAccountUserInfo{}
	if err := redis.ScanStruct(userBinded, &userinfo); err == nil {
		log.Infof("userInfo:%s",userinfo)
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		p.Resp["mobile"] = userinfo.Mobile
		p.Resp["qk_qq"] = userinfo.QkQq
		p.Resp["qk_we_chat_web"] = userinfo.QkWeChatWeb
		p.Resp["we_chat"] = userinfo.WeChat
		p.Resp["qk_we_chat"] = userinfo.QkWeChat
		return
	} else {
		p.Resp["code"] = errorcode.EC_GL_INTERNAL_ERROR
		p.Resp["msg"] = "系统内部错误"
		return
	}

}